package com.datam.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.ibatis.javassist.compiler.ast.NewExpr;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.datam.dao.CompanyInfoDAO;
import com.datam.dao.CompanyPkInfoDAO;
import com.datam.dao.ConpanyTxInfoDAO;
import com.datam.dao.SysUserDAO;
import com.datam.domain.CompanyInfoDO;
import com.datam.domain.SysUserDO;
import com.datam.query.QueryCompanyInfo;
import com.datam.query.QuerySysUser;
import com.datam.utils.DMResult;
import com.datam.utils.VerifyUtils;


@Controller
public class CompanyController {
	
	@Autowired
	CompanyInfoDAO companyInfoDAO;
	
	@Autowired
	SysUserDAO sysUserDAO ;
	
	@Autowired
	CompanyPkInfoDAO companyPkInfoDAO ; 
	
	private static Logger logger = LoggerFactory.getLogger(UserController.class);
	
	@RequestMapping("VOCsCompany/GetDataAuthorize")
	public @ResponseBody Object getDataAuthorize(HttpServletRequest request, HttpServletResponse response,
			String CompanyName,String AREA,String PkType,String REPORTINDUSTRY,String CNAME,String SHOTYPE){
		try {
			QueryCompanyInfo queryCompanyInfo = new QueryCompanyInfo();
			queryCompanyInfo.setNAME(CompanyName);
			queryCompanyInfo.setAREA(AREA);
			queryCompanyInfo.setPKTYPE(PkType);
			queryCompanyInfo.setNREPORTTYPE(REPORTINDUSTRY);
			queryCompanyInfo.setSHOWTYPE(SHOTYPE);
			SysUserDO user = (SysUserDO) request.getSession().getAttribute("user");
			String cid = (String) request.getSession().getAttribute("userCid");
			
			if(user.getType()==0&&StringUtils.isNotBlank(CNAME)){
				//找该运维企业创建的排污企业
				List<Map<String,Object>> data = companyInfoDAO.getUserByCname(CNAME);
				queryCompanyInfo.setSHOWTYPE("2");
				if(data!=null&&data.size()>0){
					Map<String,Object> map = data.get(0);
					queryCompanyInfo.setUSERID(Long.valueOf(map.get("user_id")+""));
					
				}else{
					queryCompanyInfo.setUSERID(0L);
				}
			}
			
			if(user.getType() == 1) queryCompanyInfo.setUSERID(user.getUserId());
			if(user.getType() == 2) queryCompanyInfo.setID(Long.valueOf(cid));
			List< CompanyInfoDO > data = companyInfoDAO.selectCompanyInfoList(queryCompanyInfo);
			Map r = new HashMap();
			r.put("total", data.size());
			r.put("rows", data);
			return r ;
		}catch (Exception e) {
			e.printStackTrace();
			return new DMResult("操作失败",false);
		}
	}
	
	Map <String,Integer> ids = new HashMap<>();
	@RequestMapping("VOCsCompany/GetCompanyID")
	public @ResponseBody Object GetCompanyID(HttpServletRequest request, HttpServletResponse response,
			QueryCompanyInfo info,String BASTYPE,String REPORTINDUSTRY){
		return companyInfoDAO.getseq();
		
	}
	
	@RequestMapping("VOCsCompany/test")
	public @ResponseBody Object test(HttpServletRequest request, HttpServletResponse response
			){
		CompanyInfoDO c = companyInfoDAO.selectCompanyInfoById(35L);
		c.setPSCODE("QXLPS");
		c.setWRYLB("qxlwRYLB");
		saveCompanyData( request,  response,c,"test","test",1L,"qxl1");
		c.setID(null);
		saveCompanyData( request,  response,c,"test","test",1L,"qxl2");
		GetCompanyInfoByID(request,  response,1L);
		return companyInfoDAO.getseq();
		
	}
	
	@RequestMapping("VOCsCompany/saveCompanyData")
	public @ResponseBody Object saveCompanyData(HttpServletRequest request, HttpServletResponse response,
			CompanyInfoDO companyInfoDO,String BASTYPE,String REPORTINDUSTRY,Long SHOWTYPE,String ACCOUNT){
		try {
			if(companyInfoDO.getID()==null||companyInfoDAO.selectCompanyInfoById(companyInfoDO.getID())==null){
				SysUserDO user = (SysUserDO) request.getSession().getAttribute("user");
				QuerySysUser querySysUser = new QuerySysUser();
				querySysUser.setCode(ACCOUNT);
				List< SysUserDO > userData = sysUserDAO.selectSysUserList(querySysUser);
				if(userData!=null&&userData.size()>0)return new DMResult("账号存在",false);
				companyInfoDO.setNBASTYPE(BASTYPE);
				companyInfoDO.setUSERID(user.getUserId());
				companyInfoDO.setPKTYPE("3");
				companyInfoDAO.insertCompanyInfo(companyInfoDO);
				SysUserDO u = new SysUserDO();
				u.setCode(ACCOUNT);
				u.setPassword(companyInfoDO.getPASSWORD());
				u.setCompanyInfo(companyInfoDO.getID()+"");
				u.setName(companyInfoDO.getNAME()+" 企业账号");
				if(user.getType()==0 && SHOWTYPE!=null){
					u.setType(SHOWTYPE);
				}else{
					if(SHOWTYPE==null) SHOWTYPE=2l;
					u.setType(SHOWTYPE);
				}
				sysUserDAO.insertSysUser(u);
			}else{
				companyInfoDO.setNBASTYPE(BASTYPE);
				companyInfoDAO.updateCompanyInfoById(companyInfoDO);
				if(ACCOUNT!=null){
					SysUserDO u = new SysUserDO();
					u.setPassword(companyInfoDO.getPASSWORD());
					u.setCode(ACCOUNT);
					u.setCompanyInfo(companyInfoDO.getID()+"");
					sysUserDAO.updateUserP(u);
					
				}
			}
			
		return new DMResult("插入成功",true);
		}catch (Exception e) {
				e.printStackTrace();
				return new DMResult("操作失败",false);
			}
	}
	
	@RequestMapping("VOCsCompany/GetCompanyInfoByID")
	public @ResponseBody Object GetCompanyInfoByID(HttpServletRequest request, HttpServletResponse response,
			long id ){
		try {	
				List data = new ArrayList();
				data.add(companyInfoDAO.selectCompanyInfoById(id));
				return new DMResult(data,"查询成功",true);
		}catch (Exception e) {
				e.printStackTrace();
				return new DMResult("操作失败",false);
			}
	}
	@Autowired
	ConpanyTxInfoDAO conpanyTxInfoDAO	;
	@RequestMapping("VOCsCompany/DelCompanyInfoByID")
	public @ResponseBody Object DelCompanyInfoByID(HttpServletRequest request, HttpServletResponse response,
			long id ){
		try {
			companyInfoDAO.deleteById(id);
			sysUserDAO.deleteByCompany(id);
			companyPkInfoDAO.deleteByCompany(id);
			conpanyTxInfoDAO.deleteByCompany(id);
			companyInfoDAO.deleteContact(id);
			companyInfoDAO.deleteJk(id);
			companyInfoDAO.deletePkyj(id);
			companyInfoDAO.deleteYj(id);
			companyInfoDAO.deleteZl(id);
			companyInfoDAO.deleteZYj(id);
			companyInfoDAO.deletePfl(id);
			return new DMResult("删除成功",true);
		} catch (Exception e) {
			e.printStackTrace();
			return new DMResult("操作失败",false);
		}
		
	}
	

}
